package schedule

import (
	"gitee.com/alex_li/amazon-rpt/internal/db"
	"github.com/lishimeng/app-starter"
	"github.com/lishimeng/app-starter/server"
	"github.com/lishimeng/go-log"
	"net/http"
	"time"
)

type RespItemInfo struct {
	Name     string `json:"name,omitempty"`
	Template string `json:"template,omitempty"`
	Cron     string `json:"cron,omitempty"`
	StartAt  string `json:"startAt,omitempty"`
}

func list(ctx server.Context) {

	var err error
	var resp app.ResponseWrapper

	var schedules []db.RtpSchedule
	_, err = app.GetOrm().Context.QueryTable(new(db.RtpSchedule)).
		OrderBy("id").All(&schedules)
	if err != nil {
		log.Info(err)
		resp.Code = http.StatusInternalServerError
		ctx.Json(resp)
		return
	}

	var items []RespItemInfo
	for _, schedule := range schedules {
		var item RespItemInfo
		item.Name = schedule.Name
		item.Template = schedule.Template
		item.Cron = schedule.Cron
		item.StartAt = schedule.StartAt.Format(time.DateOnly)
		items = append(items, item)
	}
	resp.Data = items
	resp.Code = http.StatusOK
	ctx.Json(resp)
}
